package com.wnxy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.system.dao.SubjectDao;
import com.wnxy.system.entity.Subject;
import com.wnxy.system.service.SubjectService;
import com.wnxy.system.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectDao, Subject> implements SubjectService {
    @Override
    public PageVo<Subject> findByPage(Map<String, Object> map) {
        //1. 获取请求参数
        String title = map.get("title").toString();
        String current = map.get("current").toString();
        String size = map.get("size").toString();
        String createTimeStart = map.get("createTimeStart").toString();
        String createTimeEnd = map.get("createTimeEnd").toString();

        //2. 构建分页对象
        IPage<Subject> pageParam = new Page<>(
                Integer.parseInt(current),Integer.parseInt(size));

        //3. 构建查询对象
        QueryWrapper wrapper = new QueryWrapper();
        if (StringUtils.hasText(title)) {
            wrapper.like("title",title);
        }
        if (createTimeStart != null) {
            wrapper.ge("create_time",createTimeStart);
        }
        if (createTimeEnd != null) {
            wrapper.le("create_time",createTimeEnd);
        }
        //4. 查询需求：根据title、创建时间范围查询
        IPage pageData = baseMapper.selectPage(pageParam, wrapper);

        //5. 封装结果
        PageVo<Subject> pageVo = new PageVo<>();
        pageVo.setList(pageData.getRecords());
        pageVo.setTotal(pageData.getTotal());
        return pageVo;
    }

    @Override
    public void delBatch(List<String> ids) {
        baseMapper.delBatch(ids);
    }
}
